﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;

using DevExpress.Utils;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Araclar
{
    public partial class GM_Init2 : XForm
    {
        public GM_Init2()
        {
            InitializeComponent();
        }

        int CustomerId;
        public GM_Init2(int CustomerId)
        {
            InitializeComponent();
            this.CustomerId = CustomerId;
        }

        private void GM_Init2_Load(object sender, System.EventArgs e)
        {
            gv.ShowFindPanel();

            bbiFoy.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;

            DataSet dsColumns = ExecuteDataSet("GM_GetColumns2", CustomerId);

            gv.Init(dsColumns.Tables[0]);
            this.Text = AppSession.Lookups.V_Customer.FindByCustomerId(CustomerId).Name;
            gv.MakeColumnInvisible("Position", "Tip", "idx", "AlwaysVisible");
            gv.SetCaptions("Position:#");
            gv.YanYanaGetirSoluna("Col", "Position", "Visible");
            GridManager gm = GridManager.GetManager(gv);
            gm.GridMenu.AddMenu("Seçili olanları görünür yap", GorunurYap);
            gm.GridMenu.AddMenu("Seçili olanları görünmez yap", GorunmezYap);

            gv.Columns[OrderFieldName].SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
            gv.OptionsCustomization.AllowSort = false;
            gv.OptionsView.ShowGroupPanel = false;

            gv.RowCellClick += gv_RowCellClick;
            gv.KeyDown += gridView1_KeyDown;
            btnKaydet.ItemClick += barButtonItem1_ItemClick;
            //gv.ShowingEditor += gv_ShowingEditor;
            gv.RowCellStyle += gv_RowCellStyle;
            gv.FocusedRowChanged += gv_FocusedRowChanged;

            gv.OptionsBehavior.ImmediateUpdateRowPosition = true;
            gv.OptionsSelection.EnableAppearanceFocusedRow = true;
            gv.Appearance.FocusedRow.BackColor = gv.Appearance.FocusedRow.BackColor2 = Color.YellowGreen;
            //gv.MakeRowSelectable();

            RepositoryItemComboBox riFoy = new RepositoryItemComboBox();
            riFoy.Items.AddRange(new object[] { "Standart", "Basit" });
            riFoy.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            bbiFoy.Edit.Assign(riFoy);

            string foytipi = dsColumns.Tables[1].Rows[0]["FoyTipi"].ToStr();
            bbiFoy.EditValue = foytipi;

            RepositoryItemComboBox riLinea = new RepositoryItemComboBox();
            riLinea.Items.AddRange(new object[] { "Model", "Kumaş" });
            riLinea.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            gv.Columns["KF"].ColumnEdit = riLinea;

            DataTable dtBand = new DataTable();
            dtBand.Columns.Add("Bant", typeof(string));
            dtBand.Columns.Add("Sira", typeof(int));
            dtBand.Rows.Add("Dikim", 1);
            dtBand.Rows.Add("İlik Düğme-1", 2);
            dtBand.Rows.Add("İlik Düğme-2", 3);
            dtBand.Rows.Add("İlik Düğme-3", 4);
            dtBand.Rows.Add("İhracat", 5);

            RepositoryItemGridLookUpEdit riBand = GetCustomGLE(dtBand, "Bant", "Sira");
            gv.Columns["Bant"].ColumnEdit = riBand;

            gv.BestFitColumns();

            chkAktarim.CheckedChanged += chkAktarim_CheckedChanged;
            chkAktarim_CheckedChanged(null, null);
        }

        void chkAktarim_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (chkAktarim.Checked)
            {
                gv.YanYanaGetirSoluna("KF", "CaptionDosya", "DosyadaZorunlu");
                gv.Columns["CaptionDosya"].Width = 100;
                gv.Columns["DosyadaZorunlu"].Width = 50;
            }
            else
                gv.MakeColumnInvisible("CaptionDosya", "DosyadaZorunlu");
        }


        void gv_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle > -1)
            {
                gv.MakeEditable("Col", OrderFieldName);
                string tip = gv.GetFocusedRowCellValue("Tip").ToStr();
                string av = gv.GetFocusedRowCellValue("AlwaysVisible").ToStr();
                string kf = gv.GetFocusedRowCellValue("KF").ToStr();

                List<string> columns = new List<string>();
                if (tip == "E" || tip == "*") columns.Add("CaptionTR");
                if (av == "E") columns.Add("Visible");
                if (kf == "*") columns.Add("KF");
                if (kf != "Kumaş") columns.Add("Bant");
                if (kf != "Kumaş" && kf != "Model") columns.Add("KFSira");

                gv.MakeColumnsReadOnly(columns.ToArray());
            }
        }

        void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt)
                switch (e.KeyCode)
                {
                    case Keys.Up:
                        UpDown(1);
                        break;
                    case Keys.Down:
                        UpDown(-1);
                        break;
                }
        }

        const string OrderFieldName = "Position";

        private void UpDown(int yon)
        {
            gv.GridControl.Focus();
            int index = gv.FocusedRowHandle;
            if (yon > 0 && index <= 0 ||
                yon < 0 && index >= gv.DataRowCount - 1)
                return;

            DataRow row1 = gv.GetDataRow(index);
            DataRow row2 = gv.GetDataRow(index - yon);
            object val1 = row1[OrderFieldName];
            object val2 = row2[OrderFieldName];
            row1[OrderFieldName] = val2;
            row2[OrderFieldName] = val1;

            gv.FocusedRowHandle = index;
        }

        void gv_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName == "CaptionTR")
                e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);

            string tip = gv.GetRowCellValue(e.RowHandle, "Tip").ToStr();
            string av = gv.GetRowCellValue(e.RowHandle, "AlwaysVisible").ToStr();
            string kf = gv.GetRowCellValue(e.RowHandle, "KF").ToStr();

            if (((tip == "E" || tip == "*") && e.Column.FieldName.StartsWith("CaptionTR") ||
                //av == "E" && e.Column.FieldName == "Visible" ||
                kf == "*" && e.Column.FieldName == "KF" ||
                kf != "Kumaş" && e.Column.FieldName == "Bant" ||
                kf != "Kumaş" && kf != "Model" && e.Column.FieldName == "KFSira")
                && e.RowHandle > -1)

                e.Appearance.BackColor = Color.Gainsboro;

        }


        void gv_ShowingEditor(object sender, CancelEventArgs e)
        {
            string tip = gv.GetFocusedDataRow()["Tip"].ToStr();
            string av = gv.GetFocusedDataRow()["AlwaysVisible"].ToStr();
            string kf = gv.GetFocusedDataRow()["KF"].ToStr();

            e.Cancel = (tip == "E" || tip == "*") && gv.FocusedColumn.FieldName.StartsWith("Caption") ||
                av == "E" && gv.FocusedColumn.FieldName == "Visible" ||
                kf == "*" && gv.FocusedColumn.FieldName == "KF" ||
                gv.FocusedColumn.FieldName == OrderFieldName ||
                gv.FocusedColumn.FieldName == "Col";
        }


        void gv_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            if (e.RowHandle > -1)
                if (e.Column == gv.Columns["RenkliMi"])
                {
                    bool currentValue = (bool)e.CellValue;

                    gv.SetRowCellValue(e.RowHandle, "RenkliMi", !currentValue);

                }
        }


        private void GorunurYap(object sender, EventArgs e)
        {
            foreach (GridCell cell in gv.GetSelectedCells())
                gv.SetRowCellValue(cell.RowHandle, "Visible", true);
        }

        private void GorunmezYap(object sender, EventArgs e)
        {
            foreach (GridCell cell in gv.GetSelectedCells())
                gv.SetRowCellValue(cell.RowHandle, "Visible", false);
        }

        void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gv.EndEditing();

            DataTable dt = gv.DataTable();
            DataRow[] drBosBant = gv.DataTable().Select("KF='Kumaş' and Bant is null");
            if (drBosBant.Length > 0)
            {
                ShowErrorMessage("Kesim föyünde KUMAŞ kısmında gözükecek olan alanların BANT kısmını belirleyin !");
                return;
            }


            //DataRow[] drAyniCaption = gv.DataTable().Select("CaptionTR = CaptionTR");
            //if (drAyniCaption.)
            //{
            //    ShowErrorMessage("CaptionTR alanı var olan giriyorsunuz. Lütfen tekrar kontrol ediniz.");
            //    return;
            //}
            string foy = bbiFoy.EditValue.ToStr();
            Session.DataManager.ExecuteNonQuery("GM_SetColumns2", CustomerId, gv.Serialize(), foy);
            //this.Close();
        }
    }
}
